define(['./../initial', './../service/showMsg'], function() {
    var app = angular.module('App.home');
    app.factory('User', ['$http', '$q',
        function($http, $q) {

            var User = function() {
                this.authorizedProjectList = [];
            };

            //把用户列表转化成按照单位分布的列表
            User.convertAuthorizedUsersInCorp = function(list) {
                var tempCorp = null;
                var tempDepart = null;
                var corpList = [];
                list.forEach(function(user) {
                    if (!tempCorp || user.corporation !== tempCorp.name) {
                        tempCorp = {
                            name: user.corporation,
                            departmentList: []
                        };
                        corpList.push(tempCorp);
                    }
                    if (!tempDepart || tempDepart.name !== user.department) {
                        tempDepart = {
                            name: user.department,
                            userList: []
                        };
                        tempCorp.departmentList.push(tempDepart);
                    }
                    tempDepart.userList.push(user);
                });
                return corpList;
            };

            User.prototype._requestHelper = function(method, action, postData) {
                if (!method || !action) return;
                var _this = this;
                var defer = $q.defer();
                action = action.replace('get', '').replace('post', '');

                var url = "/user/" + action;
                $http({
                    method: method,
                    url: url,
                    data: postData ? {
                        postData: postData, //约定postData为post的内容
                        user: _this
                    } : undefined
                }).success(function(data) {
                    if (method === 'post') {
                        data = data.toString() === "true" ? true : false;
                    }
                    defer.resolve(data);
                });
                return defer.promise;
            };

            User.prototype.hasAdminPower = function() {
                return this.authentication.code.indexOf('Administrator') >= 0 ? true : false;
            };

            User.prototype.getAppliers = function() {
                var _this = this;
                var defer = $q.defer();
                _this._requestHelper('get', 'getAppliers').then(function(applierList) {
                    applierList.forEach(function(applier, index) {
                        applierList[index] = angular.extend(new User(), applier);
                    });
                    defer.resolve(applierList);
                });
                return defer.promise;
            };

            User.prototype.postHandleApplier = function(accept) {
                accept = accept ? true : false;
                var _this = this;
                var defer = $q.defer();
                _this.accept = accept;
                var postData = {
                    applier: _this
                };
                _this._requestHelper('post', 'handleApplier', postData).then(function(result) {
                    defer.resolve(result);
                });
                return defer.promise;
            };

            //目前只是更新用户的邮箱和电话
            User.prototype.postUpdateInfo = function(infoData) {
                // infoData={phone,mail};
                var _this = this;
                var defer = $q.defer();
                _this._requestHelper('post', 'UpdateInfo', infoData).then(function(result) {
                    defer.resolve(result);
                });
                return defer.promise;
            };

            User.prototype.postChangePassword = function(postData) {
                //postData={password,newPassword}
                var _this = this;
                var defer = $q.defer();
                _this._requestHelper('post', 'ChangePassword', postData).then(function(result) {
                    defer.resolve(result);
                });
                return defer.promise;
            };

            User.prototype.getAuthorizedUsers = function() {
                var _this = this;
                var defer = $q.defer();
                _this._requestHelper('get', 'AuthorizedUsers').then(function(list) {
                    list.forEach(function(user, index) {
                        list[index] = angular.extend(new User(), user);
                    });
                    defer.resolve(list);
                });
                return defer.promise;
            };

            User.prototype.postAuthorizedProjectList = function() {
                var _this = this;
                var defer = $q.defer();
                _this._requestHelper('post', 'AuthorizedProjectList', {
                    userName: _this.userName,
                    authorizedProjectList: _this.authorizedProjectList
                }).then(function(result) {
                    defer.resolve(result);
                });
                return defer.promise;
            };

            User.prototype.postFreezeUser = function() {
                var _this = this;
                var defer = $q.defer();
                _this._requestHelper('post', 'freezeUser', {
                    freeze: _this.freeze,
                }).then(function(result) {
                    defer.resolve(result);
                });
                return defer.promise;
            };


            User.prototype.postUpdateUserAuthorizedProjectList = function(data) {
                var _this = this;
                var defer = $q.defer();
                _this._requestHelper('post', 'UpdateUserAuthorizedProjectList', data).then(function(result) {
                    defer.resolve(result);
                });
                return defer.promise;
            };

            User.prototype.postChangeUserAuthentication = function() {
                var _this = this;
                var defer = $q.defer();
                _this._requestHelper('post', 'ChangeUserAuthentication', {
                    userName: _this.userName,
                    authentication: _this.authentication.code
                }).then(function(result) {
                    defer.resolve(result);
                });
                return defer.promise;
            };

            User.prototype.getReviewProjectList = function() {
                var _this = this;
                var defer = $q.defer();
                _this
                    ._requestHelper('get', 'getReviewProjectList')
                    .then(function(result) {
                        defer.resolve(result);
                    });
                return defer.promise;
            };

            return User;
        }
    ]);
});
